package GUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class TableroVirtual extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6987436312625921375L;
	/**
	* Auto-generated main method to display this JFrame
	*/
	private int jugadorActual;
	private int tamanio;
	private JScrollPane scroll;
	private JEditorPane informacion;
	private JPanel tablero;
	private boolean juegoTerminado;
	public  String informacionJuego;
	private JButton nvo_juego;
	private int dificultad; // 1- Principiante, 2- Avanzado
	private  TableroGUI canvas;

	public TableroVirtual (int tamanio, int dificultad) {
		super();
		juegoTerminado = false;
		this.informacionJuego = "JUGADAS:\n";
		this.tamanio = tamanio;
		this.jugadorActual = 2;
		this.dificultad = dificultad;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setLocation(new java.awt.Point(-1, -1));
			{
				canvas = new TableroGUI(tamanio);
				tablero = new JPanel();
				getContentPane().add(tablero);
				tablero.setBounds(196, 12, 900, 610);
				tablero.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
				tablero.setLayout(null);
				tablero.add(canvas);
				canvas.setBounds(2 + (tablero.getWidth()/2 - canvas.tamX/2), 2+(tablero.getHeight()/2 - canvas.tamY/2), canvas.tamX, canvas.tamY);
				canvas.addMouseListener(new MouseListener() {
					
					@Override
					public void mouseReleased(MouseEvent arg0) {}
					
					
					@Override
					public void mousePressed(MouseEvent arg0) {}
					
					@Override
					public void mouseExited(MouseEvent arg0) {
						// TODO Auto-generated method stub
						
					}
					
					
					
					@Override
					public void mouseClicked(MouseEvent e) {
						if(!juegoTerminado){
						 if(canvas.realizarJugada(e.getPoint(),jugadorActual) ){
								informacionJuego += canvas.jugadaActual() + "\n";
								if(canvas.hayGanador()){
									juegoTerminado = true;
									informacionJuego += "El juego a terminado:\nGANA JUGADOR " + (jugadorActual == 3 ?"COM": jugadorActual)+".";
								}
								informacion.setText(informacionJuego);
								cambiaJugador();
								canvas.realizarJugadaVirtual(jugadorActual);
								cambiaJugador();
							}
						}
					
						
						
					}

				


					@Override
					public void mouseEntered(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
				});			
			
			}
			{
				scroll = new JScrollPane();
				getContentPane().add(scroll);
				scroll.setBounds(24, 12, 157, 610);
				{
					informacion = new JEditorPane();
					scroll.setViewportView(informacion);
					informacion.setText(informacionJuego);
					informacion.setBounds(116, 20, 154, 602);
					informacion.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
				}
			}
			{
				nvo_juego = new JButton();
				getContentPane().add(nvo_juego);
				nvo_juego.setText("Nuevo Juego");
				nvo_juego.setBounds(444, 634, 428, 33);
				nvo_juego.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						dispose();
						TableroVirtual t = new TableroVirtual(tamanio,dificultad);
						t.setLocationRelativeTo(null);
						t.setVisible(true);
						
					}
				});
			}

			pack();
			this.setSize(1130, 721);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private void cambiaJugador() {
		jugadorActual = jugadorActual == 3 ? 2: 3;
		
	}

}